package com.kaibes.core.base.thread;

public interface TimeoutThread extends RunThread {
    long getInterval();
    long getTimeOut();
    long getLastTime();
    void setLastTime(long lastTime);
    
    @Override
    default void run() {
        setRunning();
        while(isRunning()) {
            try {
                Thread.sleep(getInterval());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            long trueInterval = System.currentTimeMillis() - getLastTime();
            if (trueInterval > getInterval() && trueInterval <= getTimeOut()) {
                doWork();
            } else if (trueInterval > getTimeOut()) {
                doBroken();
                stop();
            }
        }
    }
    default void doBroken() {};
}
